<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools>
        <template #after>
          <el-button
            type="primary"
            @click="addPermission('0', 1)"
          >新增权限</el-button>
        </template>
      </page-tools>

      <el-table border :data="permissionList" row-key="id" default-expand-all>
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="是否开启">
          <template v-slot="{ row }">
            <el-switch disabled :value="row.enVisible === '1'" />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              v-if="row.type === 1"
              type="text"
              icon="el-icon-document-add"
              @click="addPermission(row.id, 2)"
            >添加</el-button>
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="editPermission(row.id)"
            >编辑</el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              style="color: #ef056e"
              @click="del(row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :title="`${showTitle}权限`"
      :visible="isShowDialog"
      @close="closeDialog"
    >
      <el-form
        ref="permissionRef"
        :model="formData"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="是否开启" prop="enVisible">
          <el-switch
            v-model="formData.enVisible"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-row type="flex" justify="center">
          <el-button size="small" type="primary" @click="btnOk">确定</el-button>
          <el-button size="small" @click="closeDialog">取消</el-button>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  addPermission,
  delPermission,
  getPermissionDetail,
  getPermissionList,
  updatePermission
} from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      isShowDialog: false,
      permissionList: [],
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' }
        ],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑' : '新增'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async del(id) {
      // 确认弹层
      await this.$confirm('是否确认删除？')
      //  执行删除的操作
      await delPermission(id)
      //  删除成功之后进行成功的提示
      this.$message.success('删除成功')
      // 重新获取列表数据
      this.getPermissionList()
    },
    addPermission(pid, type) {
      this.formData.pid = pid
      this.formData.type = type
      this.isShowDialog = true
    },
    async btnOk() {
      await this.$refs.permissionRef.validate()
      //  提交数据
      //  弹层有两个功能 添加功能/编辑功能
      //  所以提交的时候需要做判断
      if (this.formData.id) {
        await updatePermission(this.formData)
      } else {
        //
        await addPermission(this.formData)
      }
      this.$message.success(`${this.showTitle}成功`)
      // 编辑成功以后重新获取列表数据
      this.getPermissionList()
      // 关闭弹层
      this.closeDialog()
    },
    async getPermissionList() {
      // 把获取到的权限数据赋值给permissionList
      // 1、把获取到的数据转化成树形--> row数据里面必须要有children
      const res = await getPermissionList()
      this.permissionList = tranListToTreeData(res, '0')
    },
    closeDialog() {
      this.isShowDialog = false
      // 重置表单，不仅重置数据，还重置了校验
      this.$refs.permissionRef.resetFields()
      //   需要清空数据
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      }
    },
    async editPermission(id) {
      this.isShowDialog = true
      //   拿到数据 --> 请求接口
      this.formData = await getPermissionDetail(id)
    }
  }
}
</script>
